﻿Imports System.Runtime.CompilerServices
Imports System.Numerics
Module BigInteger_Exts
  <Extension()>
  Public Function Pow(ByVal X As BigInteger, ByVal y As BigInteger) As BigInteger
    If y < 0 Then Throw New NotSupportedException
    Dim RX = BigDecimals.BigDecimal.CloneObject(X)
    If y = 0 Then Return Numerics.BigInteger.One
    If y = 1 Then Return RX
    If y = 2 Then Return RX * RX
    Dim TX As BigInteger
    If y Mod 2 = 0 Then
      TX = Pow(X, y / 2)
      Return TX * TX
    End If
    TX = Pow(X, y / 2)
    Return TX * TX * RX
  End Function


End Module
